package com.knowledge.plus.io.BIO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * BIO 客户端
 *
 * @author TPF
 * @since 2025/6/4
 */
public class BioClient {
    public static void main(String[] args) throws IOException {
        try (Socket socket = new Socket("localhost", 8080);
             BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
             Scanner scanner = new Scanner(System.in)) {

            System.out.println("已连接到服务端");
            while (true) {
                System.out.print("输入消息: ");
                String msg = scanner.nextLine();
                if ("exit".equalsIgnoreCase(msg)) break;

                // 发送请求
                writer.println(msg);
                // 阻塞等待服务端响应
                String response = reader.readLine();
                System.out.println("收到响应: " + response);
            }
        }
    }
}
